import 'package:flutter/material.dart';
import 'package:flutter_axrjc/routes.dart';
import 'package:flutter_axrjc/style.dart';
import 'package:flutter_axrjc/util/Util.dart';
import 'package:flutter_axrjc/widget/CachePage.dart';
import 'package:flutter_axrjc/widget/common_widget.dart';
import 'package:flutter_axrjc/widget/dialog.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import 'logic.dart';

class MyLightPage extends StatelessWidget {
  const MyLightPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final logic = Get.find<MyLightLogic>();
    final state = Get.find<MyLightLogic>().state;

    return CachePage(
      child: Scaffold(
        body: SmartRefresher(
          header: reFreshHeader(),
          footer: loadFooter(),
          enablePullUp: true,
          enablePullDown: true,
          onLoading: () {
            logic.refreshMyLights(isRefresh: false);
          },
          onRefresh: () {
            logic.refreshMyLights(isRefresh: true);
          },
          controller: logic.refreshController,
          child: Obx(() {
            if (state.list.isEmpty) {
              return Center(child: Obx(() {
                return Text(
                  state.isNotLogin.value ? "请先登录" : "请添加灯具",
                  style: const TextStyle(color: Colors.grey),
                );
              }));
            }
            return ListView.builder(
              shrinkWrap: true,
              physics: const NeverScrollableScrollPhysics(),
              padding: EdgeInsets.zero,
              itemCount: state.list.length,
              itemBuilder: (BuildContext context, int index) {
                var bean = state.list[index];
                return Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Container(
                    padding: const EdgeInsets.all(10),
                    decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.circular(5)),
                    child: Column(
                      children: [
                        Padding(
                          padding: const EdgeInsets.symmetric(vertical: 8),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Text(
                                "${bean.fixtureName}",
                                style: const TextStyle(fontSize: 16),
                              ),
                              InkWell(
                                onTap: () {
                                  logic.newCheck(bean);
                                },
                                child: const Row(
                                  mainAxisSize: MainAxisSize.min,
                                  children: [
                                    Icon(
                                      Icons.add,
                                      color: orangeColor,
                                    ),
                                    Text(
                                      "新建检测",
                                      style: TextStyle(color: orangeColor),
                                    ),
                                  ],
                                ),
                              ),
                            ],
                          ),
                        ),
                        Padding(
                          padding: const EdgeInsets.symmetric(vertical: 8),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Row(
                                children: [
                                  const Text('照度度数:'),
                                  Text(
                                    '${(bean.brightnessAdjustment == null || bean.brightnessAdjustment?.isEmpty == true) ? 0 : bean.brightnessAdjustment}lux',
                                    style: const TextStyle(color: primaryColor),
                                  ),
                                ],
                              ),
                              Row(
                                mainAxisSize: MainAxisSize.min,
                                children: [
                                  const Text(
                                    "检测时间:",
                                    style: TextStyle(color: Color(0x90757575)),
                                  ),
                                  Text(
                                    bean.lastDetectionTime == null
                                        ? '无'
                                        : Util.date2String(Util.string2Date(
                                                "${bean.lastDetectionTime}")!) ??
                                            '',
                                    style: const TextStyle(
                                        color: Color(0x90757575)),
                                  ),
                                ],
                              ),
                            ],
                          ),
                        ),
                        const Padding(
                          padding: EdgeInsets.symmetric(vertical: 10),
                          child: Divider(
                            color: Color(0xffF3F6F9),
                          ),
                        ),
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            InkWell(
                              onTap: () {
                                Get.toNamed(MainRoutes.lightDetail,
                                    arguments: bean);
                              },
                              child: Container(
                                  padding: const EdgeInsets.all(8),
                                  decoration: BoxDecoration(
                                      borderRadius: BorderRadius.circular(5),
                                      border: Border.all(color: Colors.grey)),
                                  child: const Row(
                                    children: [
                                      Icon(
                                        Icons.remove_red_eye_outlined,
                                        size: 15,
                                      ),
                                      Text(
                                        "查看",
                                        style: TextStyle(fontSize: 12),
                                      ),
                                    ],
                                  )),
                            ),
                            InkWell(
                              onTap: () {
                                showCommonDialog(
                                    content: "确定删除灯具？",
                                    onConfirm: () {
                                      Get.back();
                                      logic.deleteLight("${bean.fixtureId}");
                                    },
                                    onCancel: () {
                                      Get.back();
                                    });
                              },
                              child: Container(
                                  padding: const EdgeInsets.all(8),
                                  decoration: BoxDecoration(
                                      borderRadius: BorderRadius.circular(5),
                                      border: Border.all(color: Colors.grey)),
                                  child: const Row(
                                    children: [
                                      Icon(
                                        Icons.delete_forever_outlined,
                                        size: 15,
                                      ),
                                      Text(
                                        "删除",
                                        style: TextStyle(
                                            color: Colors.grey, fontSize: 12),
                                      ),
                                    ],
                                  )),
                            ),
                          ],
                        )
                      ],
                    ),
                  ),
                );
              },
            );
          }),
        ),
        floatingActionButton: FloatingActionButton(
            shape: const CircleBorder(),
            child: const Icon(Icons.add),
            onPressed: () {
              Get.toNamed(MainRoutes.addLight);
            }),
      ),
    );
  }
}
